package com.yitong.equipment.base

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.gyf.immersionbar.ImmersionBar
import com.yitong.equipment.entity.other.Event
import com.yitong.equipment.network.oss.OSSService
import com.yitong.equipment.utils.ClassUtil
import com.yitong.equipment.utils.EventBusUtil
import kotlinx.coroutines.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseActivity<VM : AndroidViewModel, SV : ViewDataBinding> : AppCompatActivity() {
    // ViewModel
    protected lateinit var viewModel: VM

    // 布局view
    protected lateinit var bindingView: SV

    private var mOssService: OSSService? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (isRegisterEventBus()) {
            EventBusUtil.register(this)
        }

        //初始化沉浸式
        if (isImmersionBarEnabled()) {
            initImmersionBar()
        }
    }


    override fun setContentView(@LayoutRes layoutResID: Int) {
        bindingView = DataBindingUtil.inflate<SV>(layoutInflater, layoutResID, null, false)
        window.setContentView(bindingView!!.root)
        initViewModel()
    }

    /**
     * 初始化ViewModel
     */
    protected fun initViewModel() {
        val viewModelClass = ClassUtil.getViewModel<VM>(this)
        if (viewModelClass != null) {
            viewModel = ViewModelProvider.AndroidViewModelFactory(application)
                .create(viewModelClass)
        }
    }


    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected open fun isImmersionBarEnabled(): Boolean {
        return false
    }

    protected open fun initImmersionBar() {
        //在BaseActivity里初始化
        ImmersionBar.with(this).init()
    }


    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    open fun isRegisterEventBus(): Boolean {
        return false
    }

    protected fun getOssService(): OSSService {
        if (mOssService == null) {
            mOssService = OSSService.instance.setContext(this)
        }
        return mOssService as OSSService
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventBusCome(event: Event<Any>?) {
        if (event != null) {
            receiveEvent(event)
        }
    }


    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    open fun receiveEvent(event: Event<Any>) {

    }

    fun launch(isMain:Boolean  = false, block: suspend CoroutineScope.() -> Unit): Job {
        return lifecycleScope.launch {
            withContext(if (isMain) Dispatchers.Main else Dispatchers.IO) {
                block.invoke(this)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mOssService != null) {
            mOssService!!.reset()
        }
        if (isRegisterEventBus()) {
            EventBusUtil.unregister(this)
        }
    }


}